﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using OA.BLL;
using OA.Core;
using OA.Core.Helper;
using OA.Model;
using OA.Model.Form;
using OA.Model.Result;
using OA.Model.View;
using OA.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OA.Web.Areas.Admin.Controllers
{
    public class HomeController : BaseAdminController
    {

        private IMemoryCache _cache;
        private ICookie _cookie;
        public HomeController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
            _cookie = cookie;
        }


        public IActionResult Index()
        {
            ViewBag.Info = Info;
            return View();
        }


        public IActionResult Desk()
        {
            return View();
        }

        public IActionResult ShowInfo(string msg)
        {
            ViewBag.Msg = msg;
            return View();
        }


        [HttpGet]
        public IActionResult UpdatePassword()
        {
            return View(new UpdatePasswordForm());
        }

        [HttpPost]
        public IActionResult UpdatePassword(UpdatePasswordForm model)
        {
            if (model.NewPassWord != model.NewPassWord2)
            {
                ViewBag.Msg = "新密码与确认密码不一致!";
                return View();
            }
            AdminInfo infoUser = this.GetInfo<AdminInfo>(this.Info.ID);
            if (infoUser == null)
            {
                ViewBag.Msg = "非法的操作!";
                return View();
            }
            string md5PassWord = (infoUser.UserName+model.OldPassword).ToMd5String();
            if (infoUser.PassWord != md5PassWord)
            {
                ViewBag.Msg = "原始密码错误!";
                return View();
            }
            string md5PassWord2 = (infoUser.UserName + model.NewPassWord).ToMd5String();
            AdminInfo updateUser = new AdminInfo();
            updateUser.PassWord = md5PassWord2;
            this.Update<AdminInfo>(infoUser, updateUser, new string[] { "PassWord" });
            return FinshOther("密码更新成功");
        }

                
        [HttpGet]
        public IActionResult Logout()
        {
            _cookie.DeleteCookies(CookieHelper.AdminID);
            _cookie.DeleteCookies(CookieHelper.AdminName);
            return Redirect("/Admin/Login");
        }


 

        public IActionResult GetDepartment(int Level)
        {
            //name: '张三1', value: 1, selected: true
            try
            {
                var departments = this.dbContextFactory.DbContext.Department.Where(m => m.ParentDepartmentID == 0).Select(m => new { name = m.Name, value = m.ID, selected = false }).ToList();
                if (Level == 1)
                {
                    return Json(new
                    {
                        State = 200,
                        List = departments
                    });
                }
                var department_1 = departments.Select(m => m.value).ToList();
                departments = this.dbContextFactory.DbContext.Department.Where(m => department_1.Contains(m.ParentDepartmentID)).Select(m => new { name = m.Name, value = m.ID, selected = false }).ToList();
                if (Level == 2)
                {
                    return Json(new
                    {
                        State = 200,
                        List = departments
                    });
                }

                var department_2 = departments.Select(m => m.value).ToList();
                departments = this.dbContextFactory.DbContext.Department.Where(m => department_2.Contains(m.ParentDepartmentID)).Select(m => new { name = m.Name, value = m.ID, selected = false }).ToList();
                if (Level == 3)
                {
                    return Json(new
                    {
                        State = 200,
                        List = departments
                    });
                }

                var department_3 = departments.Select(m => m.value).ToList();
                departments = this.dbContextFactory.DbContext.Department.Where(m => department_3.Contains(m.ParentDepartmentID)).Select(m => new { name = m.Name, value = m.ID, selected = false }).ToList();
                if (Level == 3)
                {
                    return Json(new
                    {
                        State = 200,
                        List = departments
                    });
                }

                return Json(new
                {
                    State = 200,
                    Msg = "未找到对应的级别"
                });

            }
            catch(Exception ex)
            {
                return Json(new
                {
                    State = 200,
                    Msg = ex.ToString()
                });
            }
        }

        [HttpGet]
        public IActionResult GetXmSelectPositions()
        {
            List<XmSelectNode> result = new List<XmSelectNode>();
            CacheHelper cacheHelper = new CacheHelper(_cache);
            var listDepartment = cacheHelper.GetDepartments();
            var positions = cacheHelper.GetPositions();

            List<Department> topDepartments = listDepartment.Where(m => m.ParentDepartmentID == 0).ToList();
            if (topDepartments.Count == 0) { return Json(result); }

            if (topDepartments.Count == 1 && positions.Where(m => m.DepartmentID == topDepartments[0].ID).Count() == 0)//顶级节点只有一个，并且下面没有人
            {
                topDepartments = listDepartment.Where(m => m.ParentDepartmentID == topDepartments[0].ID).ToList();
            }
            foreach (var item in topDepartments)
            {
                XmSelectNode selectList = new XmSelectNode();
                selectList.value = item.ID.ToString();
                selectList.name = item.Name;
                //selectList.disabled = false;
                selectList.children = GetChildren(item.ID, listDepartment, positions, 0);
                result.Add(selectList);
            }
            return View(result);
        }
        public List<XmSelectNode> GetChildren(int departmentID, List<Department> departmentList, List<Position> listPosition, int index)
        {
            List<XmSelectNode> result = new List<XmSelectNode>();
            var departments = departmentList.Where(m => m.ParentDepartmentID == departmentID).ToList();
            var positions = listPosition.Where(m => m.DepartmentID == departmentID).ToList();
            foreach (var position in positions)
            {
                XmSelectNode selectList = new XmSelectNode();
                selectList.value = position.ID.ToString();
                selectList.name = position.Name;
                selectList.children = null;
                result.Add(selectList);
            }
            foreach (var item in departments)
            {
                XmSelectNode selectList = new XmSelectNode();
                selectList.value = item.ID.ToString();
                selectList.name = item.Name;

                selectList.children = GetChildren(item.ID, departmentList, listPosition, index);
                result.Add(selectList);
            }
            return result;

        }

        [HttpGet]
        public IActionResult GetXmSelectEmployees()
        {
            List<XmSelectNode> result = new List<XmSelectNode>();
            CacheHelper cacheHelper = new CacheHelper(_cache);
            var listDepartment = cacheHelper.GetDepartments();
            var eps = cacheHelper.GetVEmployeePositions();

            List<Department> topDepartments = listDepartment.Where(m => m.ParentDepartmentID == 0).ToList();
            if (topDepartments.Count == 0) { return Json(result); }

            if (topDepartments.Count == 1 && eps.Where(m => m.DepartmentID == topDepartments[0].ID).Count() == 0)//顶级节点只有一个，并且下面没有人
            {
                topDepartments = listDepartment.Where(m => m.ParentDepartmentID == topDepartments[0].ID).ToList();
            }
            foreach (var item in topDepartments)
            {
                XmSelectNode selectList = new XmSelectNode();
                selectList.value = item.ID.ToString();
                selectList.name = item.Name;
                selectList.children = GetEmployeeChildren(item.ID, listDepartment, eps, 0);
                result.Add(selectList);
            }
            return View(result);
        }
        public List<XmSelectNode> GetEmployeeChildren(int departmentID, List<Department> departmentList, List<VEmployeePosition> eps, int index)
        {
            List<XmSelectNode> result = new List<XmSelectNode>();
            var departments = departmentList.Where(m => m.ParentDepartmentID == departmentID).ToList();
            var currentEps = eps.Where(m => m.DepartmentID == departmentID).ToList();
            foreach (var ep in currentEps)
            {
                XmSelectNode selectList = new XmSelectNode();
                selectList.value = ep.EmpID;
                selectList.name = ep.EmpName;
                selectList.children = null;
                result.Add(selectList);
            }
            foreach (var item in departments)
            {
                XmSelectNode selectList = new XmSelectNode();
                selectList.value = "-" + item.ID.ToString();
                selectList.name = item.Name;
                selectList.children = GetEmployeeChildren(item.ID, departmentList, eps, index);
                result.Add(selectList);
            }
            return result;

        }


        [HttpGet]
        public IActionResult GetXmSelectDepartment()
        {
            List<XmSelectNode> result = new List<XmSelectNode>();
            CacheHelper cacheHelper = new CacheHelper(_cache);
            var listDepartment = cacheHelper.GetDepartments();
            var positions = cacheHelper.GetPositions();

            List<Department> topDepartments = listDepartment.Where(m => m.ParentDepartmentID == 0).ToList();
            if (topDepartments.Count == 0) { return Json(result); }

            if (topDepartments.Count == 1 && positions.Where(m => m.DepartmentID == topDepartments[0].ID).Count() == 0)//顶级节点只有一个，并且下面没有人
            {
                topDepartments = listDepartment.Where(m => m.ParentDepartmentID == topDepartments[0].ID).ToList();
            }
            foreach (var item in topDepartments)
            {
                XmSelectNode selectList = new();
                selectList.value = item.ID.ToString();
                selectList.name = item.Name;
                selectList.children = GetDepartmentChildren(item.ID, listDepartment, positions, 0);
                result.Add(selectList);
            }
            return View(result);
        }

        public List<XmSelectNode> GetDepartmentChildren(int departmentID, List<Department> departmentList, List<Position> listPosition, int index)
        {
            List<XmSelectNode> result = new List<XmSelectNode>();
            var departments = departmentList.Where(m => m.ParentDepartmentID == departmentID).ToList();
            var positions = listPosition.Where(m => m.DepartmentID == departmentID).ToList();

            foreach (var item in departments)
            {
                XmSelectNode selectList = new XmSelectNode();
                selectList.value = item.ID.ToString();
                selectList.name = item.Name;
                selectList.children = GetDepartmentChildren(item.ID, departmentList, listPosition, index);
                result.Add(selectList);
            }
            return result;

        }


    }
}